﻿Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Class AutocadAddInAutomation





    'converts the DOM into a dataset
    Public Function FormatForDataGrid(ByRef XML_Grid_Data_List As MSXML2.DOMDocument, ByVal ColumnNames() As String) As DataSet

        'declare the elements
        Dim DataSet_Temp As DataSet
        Dim DT As DataTable
        Dim DR As DataRow
        Dim DC As DataColumn
        Dim NbrColumns, NbrResults As Integer
        Dim i As Integer
        Dim Content As String

        'initiate the elements
        DT = New DataTable("ItemList")

        'declare the columns

        i = 0
        While ColumnNames(i) <> ""

            DC = New DataColumn()
            DC.DataType = System.Type.GetType("System.String")
            DC.ColumnName = ColumnNames(i)
            DC.AutoIncrement = False
            DC.Caption = ColumnNames(i)
            DC.ReadOnly = True
            DT.Columns.Add(DC)
            i = i + 1

        End While
        NbrColumns = i


        'add file status column
        DC = New DataColumn()
        DC.DataType = System.Type.GetType("System.String")
        DC.ColumnName = "Cached File"
        DC.AutoIncrement = False
        DC.Caption = "Cached File"
        DC.ReadOnly = False
        DT.Columns.Add(DC)



        'attach the table to the dataset
        DataSet_Temp = New DataSet
        DataSet_Temp.Tables.Add(DT)


        'fill in the rows
        Try
            NbrResults = XML_Grid_Data_List.getElementsByTagName("Result").item(0).childNodes.length

        Catch ex As Exception
            NbrResults = 0
        End Try

        For j = 0 To NbrResults - 1
            DR = DT.NewRow()
            For i = 0 To NbrColumns - 1
                Try
                    Content = XML_Grid_Data_List.getElementsByTagName(ColumnNames(i)).item(j).nodeTypedValue
                Catch ex As Exception
                    Content = ""
                End Try

                DR(i) = Content
            Next
            DR(i) = "Unknown"
            DT.Rows.Add(DR)
        Next


        Return DataSet_Temp
    End Function

    Public Function GetLocalFileinfo(ByVal filename As String, ByVal LocalDirectory As String) As String()
        Dim FileInfo(2) As String
        'test if file exists

        If System.IO.File.Exists(LocalDirectory & "\" & filename) Then
            'get file size
            Dim WordToAppend As String
            Dim MyFile As New System.IO.FileInfo(LocalDirectory & "\" & filename)
            FileInfo(2) = MyFile.Length
            'get file checksum


            ' code for MD5 Checksum ( http://vbdotnetforum.com/index.php?/topic/407-get-md5-of-file/ )

            Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
            Dim f As FileStream = New FileStream(LocalDirectory & "\" & filename, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
            f = New FileStream(LocalDirectory & "\" & filename, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
            md5.ComputeHash(f)
            Dim ObjFSO As Object = CreateObject("Scripting.FileSystemObject")
            Dim objFile = ObjFSO.GetFile(LocalDirectory & "\" & filename)

            Dim hash As Byte() = md5.Hash
            Dim buff As StringBuilder = New StringBuilder
            Dim hashByte As Byte
            For Each hashByte In hash
                WordToAppend = String.Format("{0:X1}", hashByte)
                If WordToAppend.Length = 1 Then
                    WordToAppend = "0" & WordToAppend
                End If
                buff.Append(WordToAppend)
            Next
            FileInfo(1) = buff.ToString()

        Else
            FileInfo(1) = ""
            FileInfo(2) = ""
        End If

        Return FileInfo
    End Function

  

End Class
